$(function () {
    var role_datagrid = $("#role_datagrid");
    var role_dialog = $("#role_dialog");
    var allData = {};
    //角色列表初始化
    role_datagrid.datagrid({
        fit: true,
        url: "/role/list",
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        striped: true,
        toolbar: "#tb",
        singleSelect: true,
        columns: [[
            {field: "x", checkbox: true},//显示多选样式
            {field: "id", title: "编号", hidden: true},
            {field: "name", title: "角色编号", width: 100},
            {field: "sn", title: "角色名称", width: 100}
        ]]
    })

    //编辑或添加对话框
    role_dialog.dialog({
        width: 540,
        height: 500,
        buttons: "#bb",
        closed:true
    });
    //所有权限
    $("#allPermissions").datagrid({
        title: "所有权限",
        width: 240,
        height: 350,
        url: "/permission/list",
        fitColumns: true,
        rownumbers: true,
        columns: [[
            {field: "name", title: "权限名称", width: 100},
        ]],
        onDblClickRow: function (index, row) {
            $("#allPermissions").datagrid("deleteRow", index);
            $("#selfPermissions").datagrid("appendRow", row)
        },
        onLoadSuccess: function (data) {
            //allData=data;
            allData = $.extend(true,{ }, data);
        }
    });
    //end


    //自身权限
    $("#selfPermissions").datagrid({
        title: "自身权限",
        width: 240,
        height: 350,
        fitColumns: true,
        rownumbers: true,
        columns: [[
            {field: "name", title: "权限名称", width: 100},
        ]],
        onDblClickRow: function (index, row) {
            $("#selfPermissions").datagrid("deleteRow", index);
            $("#allPermissions").datagrid("appendRow", row)
        },
        //数据加载成功之后,执行去除重复的操作
        onLoadSuccess: function (data) {
            //data表示加载所有数据
            //获取自己有用的权限转成Id
            var rows = $("#selfPermissions").datagrid("getRows");
            //迭代rows集合
            var ids = $.map(rows, function (row) {
                return row.id;
            })
            //获取所有的权限数组集合
            var allRows = $("#allPermissions").datagrid("getRows");

            //迭代所有权限中的选项id判断是否在ids数组中,如果在就删除
            for (var i = allRows.length - 1; i >= 0; i--) {
                if ($.inArray(allRows[i].id, ids) >= 0) {
                    $("#allPermissions").datagrid("deleteRow", i);
                }
            }
        }
    });
    //响应集合函数对象
    var cmdObj = {
        //工具栏的刷新
        reload:function(){
            role_datagrid.datagrid("load");
        },
        //工具栏编辑
        edit: function () {
            //判断是否选中了数据
            var row = role_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要编辑的数据");
                return;
            }
            $("#editForm").form("clear");
            if (row.dept) {
                row["dept.id"] = row.dept.id;
            }
            //数据回显
            $("#editForm").form("load",row);
            //如何给datagrid加url
            var options = $("#selfPermissions").datagrid("options");
            console.log(options);
            options.url='/permission/queryPermissionByRoleId?roleId='+row.id;
            //正式加载
            $("#selfPermissions").datagrid("load");

            role_dialog.dialog("open");
            role_dialog.dialog("setTitle", "角色编辑");
        },
        //工具栏添加
        add:function () {
            $("#editForm").form("clear");
            //弹框前,加载缓存数据
            $("#allPermissions").datagrid("loadData", allData);
            $("#selfPermissions").datagrid("loadData", []);
            role_dialog.dialog("open");
            role_dialog.dialog("setTitle", "角色添加");
            $("#allPermissions").datagrid("load");
        },
        //保存操作
        save:function () {
            var id = $("#roleId").val();
            var url = "/role/save";
            if (id) {
                url = "/role/update";
            }
            $("#editForm").form("submit", {
                url: url,
                onSubmit: function (param) {
                    var rows = $("#selfPermissions").datagrid("getRows");
                    for (var i = 0; i < rows.length; i++) {
                        param["permissions[" + i + "].id"] = rows[i].id;
                    }
                },
                success: function (data) {
                    data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.msg);
                    } else {
                        //关闭框
                        role_dialog.dialog("close");
                        //刷新列表数据
                        role_datagrid.datagrid("load");
                    }
                }
            })
        },
        //关闭对话框
        cancel: function () {
            role_dialog.dialog("close");
        }
    };
        //调用
        $("a[data-cmd]").click(function(){
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })
})
//工具栏编辑
/*function edit() {
 //判断是否选中了数据
 var row = role_datagrid.datagrid("getSelected");
 if (!row) {
 $.messager.alert("温馨提示", "请选择要编辑的数据");
 return;
 }
 $("#editForm").form("clear");
 if (row.dept) {
 row["dept.id"] = row.dept.id;
 }
 //数据回显
 $("#editForm").form("load", row);

 $("#allPermissions").datagrid("load");
 role_dialog.dialog("open");
 role_dialog.dialog("setTitle", "角色编辑");
 }*/

/*//工具栏添加
 function add() {
 $("#editForm").form("clear");
 //弹框前,加载缓存数据
 $("#allPermissions").datagrid("loadData", alldata);
 $("#selfPermissions").datagrid("loadData", []);
 role_dialog.dialog("open");
 role_dialog.dialog("setTitle", "角色添加");
 $("#allPermissions").datagrid("load");
 }*/
/*//保存操作
 function save() {
 var id = $("#roleId").val();
 var url = "/role/save";
 if (id) {
 url = "/role/update";
 }
 $("#editForm").form("submit", {
 url: url,
 onSubmit: function (param) {
 var rows = $("#selfPermissions").datagrid("getRows");
 for (var i = 0; i < rows.length; i++) {
 param["permissions[" + i + "].id"] = rows[i].id;
 }
 },
 success: function (data) {
 data = $.parseJSON(data);
 if (!data.success) {
 $.messager.alert('温馨提示', data.msg);
 } else {
 //关闭框
 role_dialog.dialog("close");
 //刷新列表数据
 role_datagrid.datagrid("load");
 }
 }
 })
 }*/
/*//关闭对话框
 function cancel() {
 role_dialog.dialog("close");
 }

 //列表刷新
 function reload() {
 role_datagrid.datagrid("load");
 }*/
